var e = require;
var t = module;
var o = exports;
var n = o,
    i = function (e, t, o, n) {
        return new (o || (o = Promise))(function (i, a) {
            function r(e) {
                try {
                    c(n.next(e));
                } catch (t) {
                    a(t);
                }
            }
            function s(e) {
                try {
                    c(n.throw(e));
                } catch (t) {
                    a(t);
                }
            }
            function c(e) {
                var t;
                e.done
                    ? i(e.value)
                    : ((t = e.value),
                      t instanceof o
                          ? t
                          : new o(function (e) {
                                e(t);
                            })).then(r, s);
            }
            c((n = n.apply(e, t || [])).next());
        });
    },
    a = function (e, t) {
        var o,
            n,
            i,
            a,
            r = {
                label: 0,
                sent: function () {
                    if (1 & i[0]) throw i[1];
                    return i[1];
                },
                trys: [],
                ops: []
            };
        return (
            (a = {next: s(0), throw: s(1), return: s(2)}),
            "function" == typeof Symbol &&
                (a[Symbol.iterator] = function () {
                    return this;
                }),
            a
        );
        function s(e) {
            return function (t) {
                return c([e, t]);
            };
        }
        function c(a) {
            if (o) throw new TypeError("Generator is already executing.");
            for (; r; )
                try {
                    if (
                        ((o = 1),
                        n &&
                            (i = 2 & a[0] ? n.return : a[0] ? n.throw || ((i = n.return) && i.call(n), 0) : n.next) &&
                            !(i = i.call(n, a[1])).done)
                    )
                        return i;
                    switch (((n = 0), i && (a = [2 & a[0], i.value]), a[0])) {
                        case 0:
                        case 1:
                            i = a;
                            break;
                        case 4:
                            return r.label++, {value: a[1], done: !1};
                        case 5:
                            r.label++, (n = a[1]), (a = [0]);
                            continue;
                        case 7:
                            (a = r.ops.pop()), r.trys.pop();
                            continue;
                        default:
                            if (!(i = (i = r.trys).length > 0 && i[i.length - 1]) && (6 === a[0] || 2 === a[0])) {
                                r = 0;
                                continue;
                            }
                            if (3 === a[0] && (!i || (a[1] > i[0] && a[1] < i[3]))) {
                                r.label = a[1];
                                break;
                            }
                            if (6 === a[0] && r.label < i[1]) {
                                (r.label = i[1]), (i = a);
                                break;
                            }
                            if (i && r.label < i[2]) {
                                (r.label = i[2]), r.ops.push(a);
                                break;
                            }
                            i[2] && r.ops.pop(), r.trys.pop();
                            continue;
                    }
                    a = t.call(e, r);
                } catch (s) {
                    (a = [6, s]), (n = 0);
                } finally {
                    o = i = 0;
                }
            if (5 & a[0]) throw a[1];
            return {value: a[0] ? a[1] : void 0, done: !0};
        }
    };
Object.defineProperty(n, "__esModule", {value: !0});
var r = e("AssetsMap"),
    s = e("BundleManager"),
    c = (function () {
        function e() {
            this._loadedBundleAssetss = [];
        }
        return (
            Object.defineProperty(e, "instance", {
                get: function () {
                    return e._instance || (e._instance = new e()), e._instance;
                },
                enumerable: !1,
                configurable: !0
            }),
            (e.prototype.loadResArray = function (e, t, o, n, r) {
                return i(this, void 0, Promise, function () {
                    return a(this, function () {
                        return [
                            2,
                            new Promise(function (i, a) {
                                (n ? e.preload.bind(e) : e.load.bind(e))(
                                    t,
                                    o,
                                    function (e, t) {
                                        r && r(e, t);
                                    },
                                    function (e, o) {
                                        e ? (console.error("lqr:err:" + t), a(e)) : i(o);
                                    }
                                );
                            })
                        ];
                    });
                });
            }),
            (e.prototype.reloadBubdleAssets = function (e, t) {
                return i(this, void 0, Promise, function () {
                    var o = this;
                    return a(this, function () {
                        return [
                            2,
                            new Promise(function (n, c) {
                                return i(o, void 0, void 0, function () {
                                    var o, i, l, d, u, p, h, f, _, y, m;
                                    return a(this, function (g) {
                                        switch (g.label) {
                                            case 0:
                                                if (!(o = s.default.instance.getBundle(e)))
                                                    return c("bundle not load"), [2];
                                                if (null == (i = r.default.bundleAssetsMap[e])) return [3, 8];
                                                (l = this.getBundleAssetsCount(e)), (d = 0), (g.label = 1);
                                            case 1:
                                                for (f in (g.trys.push([1, 6, , 7]),
                                                (u = function (e) {
                                                    var n, r, s, c;
                                                    return a(this, function (a) {
                                                        switch (a.label) {
                                                            case 0:
                                                                for (s in ((n = i[e]), (r = []), n.assetsList))
                                                                    r.push(n.assetsList[s]);
                                                                return r.length > 0
                                                                    ? ((c = function (e, o) {
                                                                          t && t(d + (e / o) * r.length, l);
                                                                      }),
                                                                      [4, p.loadResArray(o, r, n.assetsType, !1, c)])
                                                                    : [3, 2];
                                                            case 1:
                                                                a.sent(), (d += r.length), (a.label = 2);
                                                            case 2:
                                                                return [2];
                                                        }
                                                    });
                                                }),
                                                (p = this),
                                                (h = []),
                                                i))
                                                    h.push(f);
                                                (_ = 0), (g.label = 2);
                                            case 2:
                                                return _ < h.length ? ((y = h[_]), [5, u(y)]) : [3, 5];
                                            case 3:
                                                g.sent(), (g.label = 4);
                                            case 4:
                                                return _++, [3, 2];
                                            case 5:
                                                return this._loadedBundleAssetss.push(e), n(d), [3, 7];
                                            case 6:
                                                return (
                                                    (m = g.sent()),
                                                    game.error("loading module assets error", m),
                                                    c(m),
                                                    [3, 7]
                                                );
                                            case 7:
                                                return [3, 9];
                                            case 8:
                                                c(e + " 资源列表为空"), (g.label = 9);
                                            case 9:
                                                return [2];
                                        }
                                    });
                                });
                            })
                        ];
                    });
                });
            }),
            (e.prototype.reloadBundleAssetss = function (e, t, o, n) {
                return (
                    void 0 === t && (t = null),
                    void 0 === o && (o = null),
                    void 0 === n && (n = null),
                    i(this, void 0, void 0, function () {
                        var i, r, s, c, l, d, u;
                        return a(this, function (a) {
                            switch (a.label) {
                                case 0:
                                    for (i = 0, r = 0, s = -1, l = 0; l < e.length; l++)
                                        r += this.getBundleAssetsCount(e[l]);
                                    (c = function (e) {
                                        var t = (i + e) / r;
                                        t > s && (n && n(t), (s = t));
                                    }),
                                        (a.label = 1);
                                case 1:
                                    a.trys.push([1, 6, , 7]), (l = 0), (a.label = 2);
                                case 2:
                                    return l < e.length ? [4, this.reloadBubdleAssets(e[l], c)] : [3, 5];
                                case 3:
                                    (d = a.sent()), (i += d), (a.label = 4);
                                case 4:
                                    return l++, [3, 2];
                                case 5:
                                    return t && t(), [3, 7];
                                case 6:
                                    return (
                                        (u = a.sent()), game.error("loading module assets error", u), o && o(u), [3, 7]
                                    );
                                case 7:
                                    return [2];
                            }
                        });
                    })
                );
            }),
            (e.prototype.getBundleAssetsCount = function (e) {
                var t = r.default.bundleAssetsMap[e],
                    o = 0;
                if (t) {
                    for (var n in t) {
                        var i = t[n].assetsList;
                        o += Object.keys(i).length;
                    }
                    return o;
                }
                return 0;
            }),
            (e.prototype.getNeedLoadAssetsBundleNames = function (e) {
                for (var t = [], o = 0; o < e.length; o++)
                    -1 == this._loadedBundleAssetss.indexOf(e[o]) && t.push(e[o]);
                return t;
            }),
            (e._instance = null),
            e
        );
    })();
n.default = c;
